<%@ page import="java.sql.*" %>
<% Class.forName("com.mysql.jdbc.Driver");

String url, sql;
Connection conn;
PreparedStatement ps;

 if(request.getParameter("insert") != null)
 {
		String name = request.getParameter("sname").trim();
		String address = request.getParameter("saddress").trim();
		String telephone = request.getParameter("stelephone").trim();
		boolean proceed = false;
		
		if(name != null && address != null && telephone != null)
			if(name.length() > 0 && address.length() > 0 && telephone != null)
				proceed = true;
		
    	try 
    	{
    		Class.forName("com.mysql.jdbc.Driver");
    		url = "jdbc:mysql://localhost/student_db";
    		conn = DriverManager.getConnection(url, "root", "");
    	
    		sql = "INSERT INTO student(name, address, phone) VALUES(?, ?, ?)";
    		ps = conn.prepareStatement(sql);
    		
    		if(proceed)
    		{
    			ps.setString(1, name);
    			ps.setString(2, address);
    			ps.setString(3, telephone);
    			ps.executeUpdate();
    		}
    		
    		response.sendRedirect("http://localhost:8080/Student/Main.jsp");
    	
    	} 
    	catch (ClassNotFoundException cnfe) 
    	{
    			out.println("" + cnfe);
    	}
    	catch (SQLException sqle)
    	{
    			out.println("" + sqle);
    	}
 }
 
%>

<form method = "post" name = "insert">
Name<input type = "text" name = "sname">
<br>
Address<input type = "text" name = "saddress">
<br>
Telephone<input type = "text" name = "stelephone">
<br>
<input type = "submit" value ="Add Student" name ="insert">